package com.example.banksystem.controller;

import com.example.banksystem.entity.Account;
import com.example.banksystem.entity.User;
import com.example.banksystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin  // 允许前端跨域访问
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public String register(@RequestBody User user) {
        return userService.register(user);
    }

    @PostMapping("/login")
    public User login(@RequestParam String username, @RequestParam String password) {
        // 调用service层进行登录验证
        return userService.login(username, password);
    }
    @GetMapping("/{id}/profile")
    public Map<String, Object> getProfile(@PathVariable Long id) {
        System.out.println("请求的 userId: " + id);  // 打印日志
        User user = userService.getUserById(id);
        Account account = userService.getAccountByUserId(id);

        if(user == null || account == null) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "用户或账户不存在");
        }

        Map<String, Object> result = new HashMap<>();
        result.put("user", user);
        result.put("account", account);
        return result;
    }




}
